<?php

/**
 * transfer the mutation
 * to DP Out table
 */
$CLFetchers->fetchLstDataDetail($Db,
                                $jobId,
                                $SourceDb->fetchValue("srcDbDtl"),
                                $nDetail,
                                $lstDetail);

/**
 * since we could have multiple
 * cancelation from this tx
 * then extract how many canceled tx
 * get the source id and process
 */
$CLFetchers->fetchSubDp($Db,
                        $jobId,
                        $SourceDb->fetchValue("srcDbSubCclDpSupplier"),
                        $nSubDetail,
                        $lstSubDetail);

if($isApprove){

  for($iRow = 0; $iRow < $nSubDetail ; $iRow++){

    $CLFetchers->fetchLastMutationNo2($Db,
                                      "SOURCE",
                                      $lstSubDetail["SOURCE"][$iRow],
                                      $SourceDb->fetchValue("srcDbMutDpSupplier"),
                                      $subDtlNo);

    /**
     * cancelation is to neutralize
     * therefore we need to put
     * opposite tx Type and amount
     *
     * where done at cash bank
     * cancelation process
     */
    $values = "  ".$periode.",  ".
              " '".$lstHeader["SUPPLIER_CUSTOMER"][0]."', ".
              " '".$refId."',   ".
              " '".$lstSubDetail["SOURCE"][$iRow]."',     ".
              "  ".$hdrDate.",  ".
              "  ".$hdrDate.",  ".
              "  ".$hdrDate.",  ".
              "  ".$lstSubDetail["TX_TYPE"][$iRow].",  ".
              "  ".$lstSubDetail["AMOUNT"][$iRow]." ,  ".
              "  ".$subDtlNo." ";

    $CLHandlers->insToMutDp($Db,
                            "SUPPLIER_CODE",
                            $values,
                            $SourceDb->fetchValue("srcDbMutDpSupplier"));
  }
}else{

  for($iRow = 0; $iRow < $nSubDetail ; $iRow++){

    $CLHandlers->delEntDp($Db,
                          $refId,
                          $lstSubDetail["SOURCE"][$iRow],
                          $SourceDb->fetchValue("srcDbMutDpSupplier"));
  }
}

/**
 * write to cash bank detail mutation
 */
if($isApprove){

  $CLocal->CbTxType(_CASH_BANK_IN,
                    $cbTxType,
                    $dpTxType);

  $CLFetchers->fetchLastMutCbDtlNo($Db,
                                   $_SESSION["sysPeriode"],
                                   $SourceDb->fetchValue("srcDbMutCbDtl"),
                                   $mutCbDtlNo);

  /**
   * well since cash bank detail mutation
   * takes normal amount then
   * txType out should become x * -1
   * eg. BANK_OUT current sum is 1.000.000
   *     we have cancelation 40.000
   *
   * then NewBalance = SUM(BANK_IN) - SUM(BANK_OUT)
   *
   * where SUM(BANK_OUT) = 1.000.000 + (40.000 * -1)
   *                     =   960.000
   * thus cancelation of bank out is similar
   * to BANK_IN or any IN type transaction
   */
  $amount = $lstDetail["TOT_AMOUNT"][0] * -1;

  $values = " '".$refId."',                     ".
            " '".$lstHeader["C_B_CODE"][0]."',  ".
            "  ".$mutCbDtlNo.",   ".
            "  ".$periode.",    ".
            "  ".$hdrDate.",    ".
            " '".$lstHeader["SUPPLIER_CUSTOMER"][0]."', ".
            "  ".$txType.",                     ".
            " '".$lstHeader["CURRENCY"][0]."',  ".
            "  ".$lstHeader["RATE"][0].",       ".
            "  ".$lstDetail["TOT_AMOUNT"][0].", ".
            "  ".$lstDetail["TOT_NET"][0]."     ";

  $CLHandlers->insToMutCbDtl($Db,
                             $values,
                             $SourceDb->fetchValue("srcDbMutCbDtl"));
}else{

  $CHandlers->dbEntDelete($Db,
                          $jobId,
                          $SourceDb->fetchValue("srcDbMutCbDtl"));
}
?>